﻿/**
 * Created by arvin on 2017/4/1.
 */
/**
 * Created by arvin on 2017/3/28.
 */
var Bearcat = require('bearcat');
var Async = require("async")

module.exports = function () {
    return Bearcat.getBean({
        id: "get-motionQRCode",
        func: motionQRCode,
        props: [
            {name: "app", "ref": "application"}
        ]
    });
}

function motionQRCode() {
    this.app = null;
}

var CheckMotionDevice = function (msg) {
    if(msg.params.motion == undefined )
        return false;

    return true;
}

motionQRCode.prototype.handle = function (msg,next) {
    if(!CheckMotionDevice(msg))
    {
        next(null,{code:101,msg:110});
        return;
    }

    var model = this.app.getComponent("dao").getConnection().model("MotionDevices");
    Async.waterfall(
        [
            function () {
                model.GetDeviceData(msg.params.motion,function (data) {
                    if(data == null)
                        next(null, {code: 102, msg: "没有查询到这个ID"});
                    else
                    {
                        // device |ｕｍｅ｜南通
                        next(null,{code:1,msg:data.MotionDevice+"|"+data.MovieAddress+"|"+data.MovieHouse});
                    }
                })
            }
        ]
    );
}